<template>
  <div style="padding: 20px;">
    <el-card shadow="never" style="margin-top: 20px;">
      <el-button type="primary" @click="addOrUpdate()" size="mini">添加</el-button>
      <el-table :data="state.list" v-loading="state.loading">
        <el-table-column label="灭火器类型" prop="type" min-width="120">
          <template #default="{ row }">
            {{ paraphrase(row.type, extinguisherOption) }}
          </template>
        </el-table-column>
        <el-table-column label="合同名称" prop="contract_name" min-width="120"> </el-table-column>
        <el-table-column label="维修机构" prop="maintenance_organization" min-width="120"></el-table-column>
        <el-table-column label="服务开始时间" min-width="120" prop="service_start_time"></el-table-column>
        <el-table-column label="服务结束时间" min-width="120" prop="service_end_time"></el-table-column>
        <el-table-column label="合同状态" min-width="120">
          <template #default="{ row }">
            <el-tag :type="paraphrase(row.contract_status, contractStatusOption, 'value', 'color')">{{
              paraphrase(row.contract_status, contractStatusOption)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120">
          <template #default="{ row }">
            <el-button type="primary" link @click="addOrUpdate(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 10px;"
        background
        v-model:current-page="state.pagination.page"
        v-model:page-size="state.pagination.rows"
        layout="->,total, sizes, prev, pager, next, jumper"
        :total="state.total"
        @current-change="getList"
        @size-change="getList"
      />
    </el-card>

    <AddOrUpdate ref="addOrUpdateRef" @refresh="getList(1)"></AddOrUpdate>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRaw } from 'vue'
import { guanlihetong } from '@/api/miehuoqi'
import { paraphrase } from 'toolkit-x'
import { extinguisherOption } from '@/util/explain'
import { contractStatusOption } from '@/util/explain'
import AddOrUpdate from './components/AddOrUpdate.vue'
const addOrUpdateRef = ref()
const state = reactive({
  loading: false,
  pagination: {
    rows: 20,
    page: 1
  },
  total: 0,
  list: []
})

const getList = async (page = 1) => {
  state.loading = true
  if (page === 1) state.pagination.page = 1
  const { data } = await guanlihetong({ ...state.pagination }).finally(() => (state.loading = false))
  state.list = data.list
  state.total = data.total
}
getList()

const addOrUpdate = (row?) => {
  addOrUpdateRef.value.open(row)
}
</script>

<style lang="scss" scoped></style>
